//
//  DataTool.m
//  MarryMe
//
//  Created by lanou3g on 16/1/7.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "DataTool.h"

@implementation DataTool
#pragma mark 解析数据
+(void)requeueWithurlString:(NSString*)urlSt withHTTPMethod:(NSString*)method withHttPBody:(NSString*)body backBlock:(BackBlock)block{
    //1.创建一个网址对象
    NSURL *url=[[NSURL alloc]initWithString:urlSt];
    //2创建一个请求对象
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
    //判断是否是POST..通过请求方式PMethod 来判断是POST请求还是GET请求
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    //3创建一个回话
    NSURLSession *session=[NSURLSession sharedSession];
    //4创建一个任务(解析数据)
    NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        if (nil==error) {
            //解析数据
            id object =[NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调主线程刷新tableview(回调)
                block(object);
            });
            
            
        }else {
            NSLog(@"%@",error);
        }
        
        
    }];
    
    //执行任务
    [dataTask resume];
    
}

@end
